$(function () {
    $("#jqGrid").jqGrid({
        url: '/platform/manage/applist',
        datatype: "json",
        colModel: [
			{ label: 'APP编号', name: 'appId', index: 'appId', width: 50 ,key:true},
			{ label: 'app名称', name: 'appName', index: 'appName', width: 80 },
			// { label: 'app关键字', name: 'appKey', index: 'appKey', width: 80 },
			{ label: '所属公司', name: 'appCompany', index: 'appCompany', width: 80 },
			{ label: 'app状态', name: 'appStatus', index: 'appStatus', width: 80, formatter: function(value, options, row){
				return value === '1' ?
					'<span class="label label-danger">禁用</span>' :
					'<span class="label label-success">正常</span>';
			}},
			{ label: 'AppKey', name: 'jpushAppKey', index: 'jpushAppKey', width: 80 },
            { label: 'AppMasterSecret', name: 'jpushAppMasterSecret', index: 'jpushAppMasterSecret', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        loadComplete:function (data) {
			console.log("data",data);
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条\

        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		showInfo:false,
		title: null,
		isUpdate:false,
		app: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.showInfo = true;
			vm.title = "新增";
			vm.app = {};
            vm.isUpdate = false;
		},
		update: function (event) {
			var appGradeId = getSelectedRow();
			if(appGradeId == null){
				return ;
			}
            // vm.app = $("#jqGrid").jqGrid().getRowData(appGradeId);
            vm.showList = false;
            vm.showInfo = true;
            vm.title = "修改";
            vm.getInfo($("#jqGrid").jqGrid().getRowData(appGradeId).appId);
            vm.isUpdate = true;
		},
        saveOrUpdate: function (event) {
			var url = !vm.isUpdate ? "../sys/app/save" : "../sys/app/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.app),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var appGradeId = getSelectedRows();
			if(appGradeId == null){
				return ;
			}
			var app = $("#jqGrid").jqGrid().getRowData(appGradeId);
			var appId = app.appId;
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../sys/app/delete/"+appId,
				    // data: JSON.stringify(appIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(appId){
			$.get("../sys/app/info/"+appId, function(r){
				if (r.code == 1){
				    alert(r.msg);
                }
                vm.app = r.data;
				console.log(r.data);
            });
		},
        reload: function (event) {
            vm.showList = true;
            vm.showInfo = false;
			$("#jqGrid").jqGrid('setGridParam',{
                page:1
            }).trigger("reloadGrid");
            vm.isUpdate = false;
		}
	}
});